package com.gexiaobao.pigeon.ui.fragment.mall.evaluate

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentEvaluateListBinding
import com.gexiaobao.pigeon.ui.adapter.EvaluatedListAdapter
import com.gexiaobao.pigeon.viewmodel.EvaluateViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author      : hxw
 * @Date        : 2023/7/6 15:15
 * @Describe    : 评价列表
 */
class FragmentEvaluatedList : BaseFragment<EvaluateViewModel, FragmentEvaluateListBinding>() {

    private val mAdapter: EvaluatedListAdapter by lazy { EvaluatedListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getGoodsReviews(false)
            }
        }

        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            onRefresh()
        }
        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_goods_detail, Bundle().apply {
                putString("id", mAdapter.data[position].goodsId.toString())
            })
        }

        mAdapter.addChildClickViewIds(R.id.tvEvaluateShopName, R.id.ivEvaluateShopName, R.id.llShopName)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvEvaluateShopName, R.id.ivEvaluateShopName, R.id.llShopName -> {
                    nav().navigateAction(R.id.action_to_fragment_merchant_store, Bundle().apply {
                        putInt("shopId", mAdapter.data[position].shopId)
                    })
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

    private fun onRefresh() {
        loadsir.showLoading()
        mViewModel.getGoodsReviews(true)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.evaluatedListResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    companion object {
        fun newInstance(type: Int): FragmentEvaluatedList {
            val fragment = FragmentEvaluatedList()
            val bundle = Bundle()
            bundle.putInt("index", type)
            fragment.arguments = bundle
            return fragment
        }
    }
}